<#include "${ftl_skin}/common/header.ftl">
</head>
<body class="bg-none">
<div class="animated fadeInRight dialog-panel-body">
  <div class="popup-box"> 
   <div class="floatl col-hf-88 pre-scrollable p-r15">
    <!--panel-options end-->
    <div class="panel blank-panel">
      <div class="tab-content">
        <div class="tab-pane active" id="tab-1">
          <!--<div class="ibox-title">
                <h5> [权限]</h5>
                <div class="ibox-tools"><em>启用</em></div>
              </div>-->
              <div id="treeview1" class="test"></div>
        </div>
      </div>
    </div>
    </div>
    <!--按钮-->
    <div class="floatr col-hf-12 m-t-lg p-l15">
    	<!--<div class="m-t"><button type="button" class="btn btn-primary" onclick="checkall();">全选</button></div>
        <div class="m-t"><button type="button" class="btn btn-primary" onclick="recheck();">反选</button></div>-->
    	<div class="m-t"><button type="button" class="btn btn-primary" onClick="save();">保存</button></div>
        <div class="m-t"><button type="button" class="btn btn-primary" onClick="resetTree();">重置</button></div>
    </div>
    <!--按钮-->
  </div>
</div>
<#include "${ftl_skin}/common/js.ftl">
<script src="${path.skinPath }js/demo/layer-demo.min.js"></script> 
<script src="${path.skinPath }js/plugins/treeview/bootstrap-treeview.js"></script> 
<script src="${path.skinPath }js/plugins/sweetalert/sweetalert.min.js"></script> 
<script type="text/javascript" >
	$(function() {
		$("#treeview1").treeview({
			data: getTreeData("${path.servicePath}dept/treelistauth",{}),
			showTags: !0,
			showCheckboxs: !0
		});
	});
	
	function getTreeData(url,params){
		var treeList,treeData;
    
    HFAjax("${path.servicePath}dept/treelistauth.do", params, function(data){
        if(data.state == 0){
           treeList = data.result;
        }
    });

		if(treeList.length>0){
			for(var i = 0 ; i < treeList.length ; i++){
				if(i < 0){
					continue;
				}else{
					//treeList[i].checkboxs= [,{"name":"use","checked":"true"}];
					var isChecked = treeList[i].is_auth == '1' ;
					//var isDisabled = treeList[i].is_disabled == '1';
					treeList[i].checkboxs= [{"name":"use","checked":isChecked}];
				}
			}
			
			treeData = toTree(treeList,"-1");
		}
		
		return treeData;
	}
	
	function tree_botton_bind(){
	}
	
	
	function getData(viewid){
		var array=[];
		
		var treeview = $("#"+viewid).data("plugin_treeview");
		
		var tree_list= [];
		$.each(treeview.tree,function(index,nodes){
			treeview._jsonTreeToList(nodes,tree_list);
		});
		
		var nodeIdArray = [];
		
		$.each(tree_list,function(nodeid,node){
		
			if($.inArray(node.id,nodeIdArray)){
				nodeIdArray.push(node.id);

		
				var checkChanged = 0;
				//var node = $(this).data("node-info");
				var ischecked = 0;
				
				$.each(node.checkboxs,function(i,checkboxObj){
					
					if (checkboxObj.changed && (checkboxObj.changed == true ||  checkboxObj.changed == 'true')) {
						checkChanged=1;
					}
					
					if(checkboxObj.name == "use" && checkboxObj.checked == true){
						ischecked = 1 ;
					}
				
				});
				
				if(checkChanged==1){
					var dataObj = {
							"edit":node.is_edit,
							"func_id":node.id ,
							"node_name":node.node_name ,
							"tree_code":node.tree_code ,
							"is_auth":ischecked ,
						};
					array.push( dataObj );
				}
			
			}
				
		});
		
		//console.log(JSON.stringify(array));
		
		return array;
	}
	
	function save(){
		var returnVal={
			date:getData("treeview1")
		};
		closeDialog(returnVal);
	}
	
	
	function resetTree(){
		var treeid = $(".nav-tabs").find(".active").attr("data-treeid");
		$("#treeview1").treeview({
			data: getTreeData("${path.servicePath}/dept/treelistauth.do",{}),
			showTags: !0,
			showCheckboxs: !0
		});
	}
	
	//=================================== list 转　树结构　==================================
	function toTree(data, pid) {
		var result = [], temp;
		for (var i = 0; i < data.length; i++) {
			if (data[i].parent_tree_code == pid) {
				var obj = data[i];
				obj.id = data[i].id;
				obj.text = data[i].node_name;
				temp = toTree(data, data[i].tree_code);
				if (temp.length > 0) {
					obj.nodes = temp;
				}
				
				result.push(obj);
			}
		}
		
		return result;
	}
	
</script> 
<#include "${ftl_skin}/common/footer.ftl"> 